#include "Ark.h"
#include "StringOps.h"
#include "Neptuner.h"

//Crude singleton
static Ark gxArk;

void Ark_addScuba(Scuba* pxScuba)
{
	gxArk.m_apxScuba.push_back(pxScuba);
}

Radar* Ark_getRadar()
{
	return &(gxArk.m_xRadar);
}

std::vector<Scuba*>* Ark_getScubaPack()
{
	return &(gxArk.m_apxScuba);
}

std::string Ark_getAbsolutePath(std::string sFilename)
{
	bfs::path xTest;
	xTest = bfs::complete(bfs::path("./" + sFilename,bfs::native));
	xTest = bfs::system_complete(xTest);
	if(bfs::exists(xTest))
		return xTest.string();

	return gxArk.m_xRadar.getAbsolutePath(sFilename);
}

Scuba* Ark_isParsed(std::string sFilename)
{
	size_t i=0, n= gxArk.m_apxScuba.size();
	Scuba* pxItem;

	for(i=0;i<n;i++)
	{
		pxItem=gxArk.m_apxScuba[i];
		if(pxItem->m_sFilename == sFilename)
		{
			return pxItem;
		}
	}
	return NULL;
}

std::string Ark_getCodebaseLocation(std::string sFullPath)
{
	return gxArk.m_xRadar.getCodebaseLocation(sFullPath);
}

std::string Ark_getLinkForSymbol(Symbol xSymbol, Scope* pxScope, int nCurrentLevel)
{
	std::string sTokenHTML = xSymbol.m_sName;

	if(!pxScope->findSymbol(sTokenHTML, xSymbol))
	{
		return sTokenHTML;
	}

	sTokenHTML = "<a href='";
	std::string sTargetFile = Ark_getCodebaseLocation(xSymbol.m_xDefinition.m_sFilename);
	sTargetFile += ".uboat.html";
	std::string sRelativizer = Neptuner_getRelativizer(nCurrentLevel)+"codebase/";
	sTargetFile = sRelativizer + sTargetFile;
	sTokenHTML += sTargetFile;
	sTokenHTML += "#SL";
	sTokenHTML += stringify(xSymbol.m_xDefinition.m_nLineNumber);
	sTokenHTML += "'>";
	sTokenHTML += xSymbol.m_sName;
	sTokenHTML += "</a>";

	return sTokenHTML;
}